package com.qymerge.manage.base.dao.impl;

import com.qymerge.manage.base.dao.IBDMVoucherDao;
import com.qymerge.manage.base.dao.ICardDao;
import com.qymerge.manage.base.ro.BDMVoucherRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.constant.ParamKey;
import com.qymerge.manage.core.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *<p>Title: BDMVoucherDaoImpl</p>
 *<p>Description: 财务凭证</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2025/06/13
 *@Time 17:54
 */
@Repository
public class BDMVoucherDaoImpl extends BaseDao implements IBDMVoucherDao {

    private String dwd_MCRS_BDM_Voucher = "SELECT Id,CompanyId,PeriodId,PeriodCode,PeriodYear,PeriodMonth,VoucherTypeId,VoucherTypeName,SourceSys,SourceType,AROtherBillType,BookedDate,BizStatus,JournalEntryId,JournalEntryAuxiliaryAcc_id,CustomerId,CustomerInternalCompanyId,SupplierId,SupplierInternalCompanyId,CostCenterId,CostCenterName,AccountViewCode,AccountViewName,GroupAccountViewCode,Debit,Credit,Remark,VoucherId,VoucherCode,SourceOperation,Etl_Last_Date,PeriodDate from dwd_MCRS_BDM_Voucher where 1=1 ";

    @Override
    public List list(BDMVoucherRO ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = dwd_MCRS_BDM_Voucher;
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(ro.getVoucherTypeName())) {
            sql += " and VoucherTypeName like :VoucherTypeName";
            paramMap.put("VoucherTypeName", "%" + ro.getVoucherTypeName() + "%");
        }
        if (StringUtils.isNotBlank(ro.getCostCenterName())) {
            sql += " and CostCenterName like :CostCenterName";
            paramMap.put("CostCenterName", "%" + ro.getCostCenterName() + "%");
        }
        if (StringUtils.isNotBlank(ro.getAccountViewCode())) {
            sql += " and AccountViewCode like :AccountViewCode";
            paramMap.put("AccountViewCode", "%" + ro.getAccountViewCode() + "%");
        }
        if (StringUtils.isNotBlank(ro.getAccountViewName())) {
            sql += " and AccountViewName like :AccountViewName";
            paramMap.put("AccountViewName", "%" + ro.getAccountViewName() + "%");
        }
        if (StringUtils.isNotBlank(ro.getVoucherCode())) {
            sql += " and VoucherCode like :VoucherCode";
            paramMap.put("VoucherCode", "%" + ro.getVoucherCode() + "%");
        }

        // 分页数据获取
        sql += " order by Id desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return getNamedParameterJdbcTemplate().query(sql, paramMap, new BeanPropertyRowMapper<BDMVoucherRO>(BDMVoucherRO.class));
    }

    @Override
    public int total(BDMVoucherRO ro) {
        String sql = "select count(1) from dwd_MCRS_BDM_Voucher where 1=1";
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(ro.getVoucherTypeName())) {
            sql += " and VoucherTypeName like :VoucherTypeName";
            paramMap.put("VoucherTypeName", "%" + ro.getVoucherTypeName() + "%");
        }
        if (StringUtils.isNotBlank(ro.getCostCenterName())) {
            sql += " and CostCenterName like :CostCenterName";
            paramMap.put("CostCenterName", "%" + ro.getCostCenterName() + "%");
        }
        if (StringUtils.isNotBlank(ro.getAccountViewCode())) {
            sql += " and AccountViewCode like :AccountViewCode";
            paramMap.put("AccountViewCode", "%" + ro.getAccountViewCode() + "%");
        }
        if (StringUtils.isNotBlank(ro.getAccountViewName())) {
            sql += " and AccountViewName like :AccountViewName";
            paramMap.put("AccountViewName", "%" + ro.getAccountViewName() + "%");
        }
        if (StringUtils.isNotBlank(ro.getVoucherCode())) {
            sql += " and VoucherCode like :VoucherCode";
            paramMap.put("VoucherCode", "%" + ro.getVoucherCode() + "%");
        }

        return getNamedParameterJdbcTemplate().queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public BDMVoucherRO findOne(Long id) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_BDM_Voucher);
        sql.append(" and Id=?");
        RowMapper<BDMVoucherRO> rowMapper = new BeanPropertyRowMapper<BDMVoucherRO>(BDMVoucherRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, id);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public int install(BDMVoucherRO ro) {
        StringBuilder sql = new StringBuilder("insert into dwd_MCRS_BDM_Voucher(CompanyId,PeriodId,PeriodCode,PeriodYear,PeriodMonth,VoucherTypeId,VoucherTypeName,SourceSys,SourceType,AROtherBillType,BookedDate,BizStatus,JournalEntryId,JournalEntryAuxiliaryAcc_id,CustomerId,CustomerInternalCompanyId,SupplierId,SupplierInternalCompanyId,CostCenterId,CostCenterName,AccountViewCode,AccountViewName,GroupAccountViewCode,Debit,Credit,Remark,VoucherId,VoucherCode,SourceOperation,GroupAccountViewCodeBelong) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

        List params = new ArrayList();
        params.add(ro.getCompanyId());
        params.add(ro.getPeriodId());
        params.add(ro.getPeriodCode());
        params.add(ro.getPeriodYear());
        params.add(ro.getPeriodMonth());
        params.add(ro.getVoucherTypeId());
        params.add(ro.getVoucherTypeName());
        params.add(ro.getSourceSys());
        params.add(ro.getSourceType());
        params.add(ro.getAROtherBillType());
        params.add(ro.getBookedDate());
        params.add(ro.getBizStatus());
        params.add(ro.getJournalEntryId());
        params.add(ro.getJournalEntryAuxiliaryAcc_id());
        params.add(ro.getCustomerId());
        params.add(ro.getCustomerInternalCompanyId());
        params.add(ro.getSupplierId());
        params.add(ro.getSupplierInternalCompanyId());
        params.add(ro.getCostCenterId());
        params.add(ro.getCostCenterName());
        params.add(ro.getAccountViewCode());
        params.add(ro.getAccountViewName());
        params.add(ro.getGroupAccountViewCode());
        params.add(ro.getDebit());
        params.add(ro.getCredit());
        params.add(ro.getRemark());
        params.add(ro.getVoucherId());
        params.add(ro.getVoucherCode());
        params.add(ro.getSourceOperation());
        params.add(ro.getGroupAccountViewCodeBelong());

        return getJdbcTemplate().update(sql.toString(), params.toArray());
    }

    @Override
    public int update(BDMVoucherRO ro) {
        StringBuilder sql = new StringBuilder("update dwd_MCRS_BDM_Voucher set CompanyId=:CompanyId,PeriodId=:PeriodId,PeriodCode=:PeriodCode,PeriodYear=:PeriodYear,PeriodMonth=:PeriodMonth,VoucherTypeId=:VoucherTypeId,VoucherTypeName=:VoucherTypeName,SourceSys=:SourceSys,SourceType=:SourceType,AROtherBillType=:AROtherBillType,BookedDate=:BookedDate,BizStatus=:BizStatus,JournalEntryId=:JournalEntryId,JournalEntryAuxiliaryAcc_id=:JournalEntryAuxiliaryAcc_id,CustomerId=:CustomerId,CustomerInternalCompanyId=:CustomerInternalCompanyId,SupplierId=:SupplierId,SupplierInternalCompanyId=:SupplierInternalCompanyId,CostCenterId=:CostCenterId,CostCenterName=:CostCenterName,AccountViewCode=:AccountViewCode,AccountViewName=:AccountViewName,GroupAccountViewCode=:GroupAccountViewCode,Debit=:Debit,Credit=:Credit,Remark=:Remark,VoucherId=:VoucherId,VoucherCode=:VoucherCode,SourceOperation=:SourceOperation, GroupAccountViewCodeBelong=:GroupAccountViewCodeBelong ");

        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("CompanyId", ro.getCompanyId());
        paramsMap.put("PeriodId", ro.getPeriodId());
        paramsMap.put("PeriodCode", ro.getPeriodCode());
        paramsMap.put("PeriodYear", ro.getPeriodYear());
        paramsMap.put("PeriodMonth", ro.getPeriodMonth());
        paramsMap.put("VoucherTypeId", ro.getVoucherTypeId());
        paramsMap.put("VoucherTypeName", ro.getVoucherTypeName());
        paramsMap.put("SourceSys", ro.getSourceSys());
        paramsMap.put("SourceType", ro.getSourceType());
        paramsMap.put("AROtherBillType", ro.getAROtherBillType());
        paramsMap.put("BookedDate", ro.getBookedDate());
        paramsMap.put("BizStatus", ro.getBizStatus());
        paramsMap.put("JournalEntryId", ro.getJournalEntryId());
        paramsMap.put("JournalEntryAuxiliaryAcc_id", ro.getJournalEntryAuxiliaryAcc_id());
        paramsMap.put("CustomerId", ro.getCustomerId());
        paramsMap.put("CustomerInternalCompanyId", ro.getCustomerInternalCompanyId());
        paramsMap.put("SupplierId", ro.getSupplierId());
        paramsMap.put("SupplierInternalCompanyId", ro.getSupplierInternalCompanyId());
        paramsMap.put("CostCenterId", ro.getCostCenterId());
        paramsMap.put("CostCenterName", ro.getCostCenterName());
        paramsMap.put("AccountViewCode", ro.getAccountViewCode());
        paramsMap.put("AccountViewName", ro.getAccountViewName());
        paramsMap.put("GroupAccountViewCode", ro.getGroupAccountViewCode());
        paramsMap.put("Debit", ro.getDebit());
        paramsMap.put("Credit", ro.getCredit());
        paramsMap.put("Remark", ro.getRemark());
        paramsMap.put("VoucherId", ro.getVoucherId());
        paramsMap.put("VoucherCode", ro.getVoucherCode());
        paramsMap.put("SourceOperation", ro.getSourceOperation());
        paramsMap.put("GroupAccountViewCodeBelong", ro.getGroupAccountViewCodeBelong());

        sql.append("where Id=:Id");
        paramsMap.put("Id", ro.getId());
        return getNamedParameterJdbcTemplate().update(sql.toString(), paramsMap);
    }


}
